<?php

class myUser extends sfGuardSecurityUser
{
	public function setFlashNotice($notice){
		$this->setFlash('notice',$notice);
	}
	public function setFlashError($error){
		$this->setFlash('error',$error);
	}

	public function getBranchId( ){
		$profile   = $this->isAuthenticated() && $this->getGuardUser() && $this->getGuardUser()->getProfile()? $this->getGuardUser()->getProfile() : null;

		$branch_id = $profile && $profile->getBranchId()? $profile->getBranchId() : globals::DEMO_BRANCH_ID;
		return $branch_id;
	}

	public function getDefaultDepartmentId( $rand = false){
		/*@var $g_user sfGuardUser*/
		$g_user = $this->getGuardUser();
		$c= new Criteria();
		$c->add(CodeDepartmentPeer::BRANCH_ID ,$this->getBranchId());
		$c->addDescendingOrderByColumn(CodeDepartmentPeer::ID);
		if(!$rand){
			$obj = CodeDepartmentPeer::doSelectOne($c);
			return $obj ? $obj->getId() : globals::DEMO_DEPT_ID;
		}else{
			$objs = CodeDepartmentPeer::doSelect($c);

			if(is_array($objs)){
				shuffle($objs);
				return $objs[0]->getId();
			}else{
				return globals::DEMO_DEPT_ID ;
			}

		}	}

	public function getDefaultSectionId( $rand = false ){
		/*@var $g_user sfGuardUser*/
		$g_user = $this->getGuardUser();
		$c= new Criteria();
		$c->add(CodeSectionPeer::BRANCH_ID ,$this->getBranchId());
		$c->addDescendingOrderByColumn(CodeSectionPeer::ID);
		if(!$rand){
			$obj = CodeSectionPeer::doSelectOne($c);
			return $obj ? $obj->getId() : globals::DEMO_SECTION_ID;
		}else{
			$objs = CodeSectionPeer::doSelect($c);

			if(is_array($objs)){
				shuffle($objs);
				return $objs[0]->getId();
			}else{
				return globals::DEMO_SECTION_ID;
			}

		}
	}

}
